<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Individual Report</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
    <link rel="stylesheet" type="text/css" href="./jquery/easyui.css">
    <link rel="stylesheet" type="text/css" href="./jquery/icon.css">
</head>
<?php
	define('CONFIG_FILE',true);
	include './config.php';
	define('DB_FILE',true);
	
	session_start();
	if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
		Header("Location: ./Index.php");
		exit();
	}else{
		$userType = $_SESSION["userType"];
	}
	
	// set timeout period in seconds
	$inactive = 7200;
	// check to see if $_SESSION['timeout'] is set
	if (isset($_SESSION["timeout"])) {
		$session_life = time() - $_SESSION["timeout"];
		if ($session_life > $inactive) {
			session_destroy();
			Header("Location: Timeout.php");
		}
	}
	$_SESSION["timeout"] = time();
?>        
<body>
    <div class="banner">
	</div>
    <div id="main">
        <div class="left-wrapper">
            <div class="logo">
                <img src="images/eldercare.gif" alt="logo" width="240px" />
            </div>
			<div class="menu-wrapper">
				<div id="cssmenu">
					<?php
						if ($userType == "Therapy Assistant") {
							include_once('./Sidebar/_SidebarTA.php');
						} else if ($userType == "Centre Manager") {
							include_once('./Sidebar/_SidebarCM.php');
						} else {
							include_once('./Sidebar/_SidebarSA.php');
						}
					?>
				</div>
			</div>
		</div>           
		<div id="content">
			<div class="content2">
				<b style="float:right; margin: 0px 0 0 0">  
				<?php
					//session_start();
					if (!empty($_SESSION["centreName"])) {
						$Username = $_SESSION["username"];
						$centreName = $_SESSION["centreName"];
						echo "Welcome, " . $Username . " ! " . $centreName . " Branch";
					} else {
						$Username = $_SESSION["username"];
						echo "Welcome, " . $Username;
					}
				?>
				<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
				</b>
				</p>
				<br>
			<div style="float:center">
				<br>
				<h3>Existing Clients</h3>
				<br>						
				<table id="dg" title="Clients" class="easyui-datagrid" style="width:auto;height:430px; "
					   url="./IndividualRpts/get_clients.php"
					   toolbar="#toolbar" view="scrollview"
					   rownumbers="true" fitColumns="true">
					<thead>
						<tr>
							<th field="ck" checkbox="true"></th>
							<th field="NRIC" width="50" sortable="true">NRIC</th>
							<th field="CustomerName" width="50" sortable="true">Name</th>
							<th field="Gender" width="50" sortable="true">Gender</th>
							<th field="Citizenship" width="50" sortable="true">Citizenship</th>
							<th field="BeforeDeviation" width="50"sortable="true">Before Deviation</th>
							<th field="AfterDeviation" width="50"sortable="true">After Deviation</th>
							<th field="DateOfBirth" width="50"sortable="true">Date of Birth</th>
							<th field="CentreName" width="50" sortable="true">Centre Name</th>
						</tr>
					</thead>
				</table>
				<div id="toolbar">
					<?php
						require_once './Tool/DB/SqlDBManager.class.php';
						if ($userType == "System Admin") {
							echo "<span>Centre:</span>";
							echo "<select id=\"ddlCentre\" name=\"ddlCentre\" class=\"easyui-combobox\"style=\"width:155\" height=\"auto\">";
							echo"<option value='0'selected>All</option>";							
							$sqlDBManager = new SqlDBManager();
							$sql="Select CentreId, CentreName FROM centre";
							$res=$sqlDBManager->queryRows($sql);
							if(!empty($res)){
								for($i=0;$i<count($res);$i++){
									echo "<option value='".$res[$i]['CentreId']."'>".$res[$i]['CentreName']."</option>";
								}
							}									
							echo "</select>";
						}					
					?>
					<a href='#' class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="individualRpt()">Generate Report</a>
					<input id="searchid" name="searchid" type="text" style="border-radius:25px">
					<a href="#" class="easyui-linkbutton" iconCls="icon-search" onClick="getClient()">Search</a>
					<a href="#" class="easyui-linkbutton" onClick="findAll()">All</a>
				</div>
			</div>
			</div>
		</div>
		<div class="clear"></div>
	</div>
	<div id="footer"> &copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
</body> 
<script type="text/javascript" src="./jquery/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="./jquery/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="./jquery/jquery.easyui.min.js"></script>
<script type="text/javascript">
	var url;
	function getClient() {
		//validate search input 
		var flagCentreLocation = 0;   
        var flagSearchId = 0;
        $.ajax({
            async : false,
            type : "POST",
            url : "./IndividualRpts/validateSearch.php", //the page/method you wanna validate
            data : {
				CentreLocation: $('[name="ddlCentre"]').val(),
				searchid: $('#searchid').val()
			},
            success : function(data) {
            	var data = JSON.parse(data);	
				flagCentreLocation=data.flagCentreLocation;
				flagSearchId=data.flagSearchId;
            }
        });
        if(flagCentreLocation==0){
            $.messager.show({
                title: 'Error',
				msg: 'Invalid Centre Id. Centre Id cannot contain any special characters and letters.',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
            });
        }else if(flagSearchId==0){
            $.messager.show({
                title: 'Error',
                msg: 'Invalid Search Input. Search Input cannot contain any special characters except brackets, dashes and single quatation marks.',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
            });
        }else{
        	$('#dg').datagrid('load', {
        		CentreLocation: $('[name="ddlCentre"]').val(),
				searchid: $('#searchid').val()
			});	
        }
	}
	
	function findAll() {
		$('#dg').datagrid('load', {
		});
	}
	function individualRpt() {
		var nricList = [];
		var rows = $('#dg').datagrid('getSelections');
		for(var i=0; i<rows.length; i++){
			nricList.push(rows[i].NRIC);
		}

		//convert array to json
		var jsonArray = JSON.stringify(nricList);
		//prepare POST data
		var dataToPost = {'nricList':jsonArray };

		var flag=0;
		var queryString;
		//send POST data to PHP and handle response
		$.ajax({
		  async : false,
		  type: 'POST',
		  url: './IndividualRpts/storeNRICList.php',
		  data: dataToPost,
		  success : function(data) {
            var data = JSON.parse(data);	
            flag=data.emptyFlag; 
			queryString=data.queryString; 	
		  }	  
		});
		if(flag==0){
			$.messager.show({
                title: 'Error',
				msg:'Please select at least one client to proceed.',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
            });
		}else{	
			var win = $.messager.progress({
                title:'Please waiting',
                msg:'Processing data...',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
            });
            setTimeout(function(){
                $.messager.progress('close');
                window.location="./IndividualProcess.php?queryString="+queryString;
            },3000)
	   	}		
	}
</script>	
</html>